﻿Imports System.Text
Imports System.Security.Cryptography
Imports System.IO
Public Class Common
    Public Function EncryptPasswordMD5(ByVal Password As String) As String
        Dim retPassword As String = ""
        Dim u As New UTF32Encoding
        Dim bytes As Byte() = u.GetBytes(Password)
        Dim MD5 As New MD5CryptoServiceProvider
        Dim strmd5 As Byte() = MD5.ComputeHash(bytes)
        Dim strSHA1 As Byte() = u.GetBytes(Convert.ToBase64String(strmd5))
        Dim sha1 As New SHA1CryptoServiceProvider
        Dim kq As Byte() = sha1.ComputeHash(strSHA1)
        Return Convert.ToBase64String(kq)
    End Function
    Public Function Decrypt(ByVal EncryptString As String) As String
        Try
            Dim Password As String = "!@#dangthingoctram$%^"
            Dim managed As New RijndaelManaged
            Dim buffer As Byte() = Convert.FromBase64String(EncryptString)
            Dim rgbSalt As Byte() = Encoding.ASCII.GetBytes(Password.Length.ToString)
            Dim bytes As New PasswordDeriveBytes(Password, rgbSalt)
            Dim transform As ICryptoTransform = managed.CreateDecryptor(bytes.GetBytes(&H20), bytes.GetBytes(&H10))
            Dim stream As New MemoryStream(buffer)
            Dim stream2 As New CryptoStream(stream, transform, CryptoStreamMode.Read)
            Dim buffer3 As Byte() = New Byte(stream.Length - 1) {}
            Dim count As Integer = stream2.Read(buffer3, 0, buffer3.Length)
            stream.Close()
            stream2.Close()
            Return Encoding.Unicode.GetString(buffer3, 0, count)
        Catch ex As Exception
            Return ""
        End Try

    End Function
    Public Function Encrypt(ByVal Username As String) As String
        Dim sRet As String = ""
        Dim inArray As Byte()
        Try
            Dim Password As String = "!@#dangthingoctram$%^"
            Dim managed As New RijndaelManaged
            Dim buffer As Byte() = Encoding.Unicode.GetBytes(Username)
            Dim rgbSalt As Byte() = Encoding.ASCII.GetBytes(Password.Length.ToString)
            Dim bytes As New PasswordDeriveBytes(Password, rgbSalt)
            Dim transform As ICryptoTransform = managed.CreateEncryptor(bytes.GetBytes(&H20), bytes.GetBytes(&H10))
            Dim stream As New MemoryStream
            Dim stream2 As New CryptoStream(stream, transform, CryptoStreamMode.Write)
            stream2.Write(buffer, 0, buffer.Length)
            stream2.FlushFinalBlock()
            inArray = stream.ToArray
            stream.Close()
            stream2.Close()
        Catch ex As Exception

        End Try
        Try
            sRet = Convert.ToBase64String(inArray)
        Catch ex As Exception

        End Try
        Return sRet
    End Function
End Class
